<?php
class localisation_models_index extends CI_Model{
	public function __construct(){
		$this->load->database();
	}
	//prendre le nom de région
	public function get_region_by_departement($codeDepartement=''){
		$codeDepartement=trim($codeDepartement);
		$this->db->select('*')->
		from('departement_region');
		if(!empty($codeDepartement)){
			$this->db->like('code_departement', ",$codeDepartement,");
			$this->db->or_like('code_departement', ",$codeDepartement",'before');
			$this->db->or_like('code_departement', "$codeDepartement,",'after');
		}
		$get= $this->db->get();
		if($get->num_rows()){
			$rows= $get->result();
			return $rows[0]->code_departement;
		}
		return false;
	}
	//retourne code region si on connais code de departement
	public function get_region($codeDepartement=''){
		$codeDepartement=trim($codeDepartement);
		$this->db->select('*')->
		from('departement_region');
		if(!empty($codeDepartement)){
			$this->db->like('code_departement', ",$codeDepartement,");
			$this->db->or_like('code_departement', ",$codeDepartement",'before');
			$this->db->or_like('code_departement', "$codeDepartement,",'after');
		}

		$get= $this->db->get();
		return $get->result();
	}
	//prendre le nom de département si on connait code departement
	public function get_departement($codedepartement=''){
		$this->db->select('*')->
		from('departement_nom');
		if(!empty($codedepartement)) $this->db->where_in('code_departement', explode(',', $codedepartement));
		$get= $this->db->get();
		return $get->result();
	}
	//avoir les noms des villes si on connait code departement
	public function get_ville($codeDepartement){
		$this->db->select('*')->
		from('departement_ville')->order_by("N_Com_min", "asc");
		if(!empty($codeDepartement)) $this->db->like('cp', $codeDepartement, 'after');
		$get= $this->db->get();
		return $get->result();
	}
	//permetre d'avoir codepostal si on connait le nom de ville
	public function get_cp_by_city($city){
		$this->db->select('*')->
		from('departement_ville')->where("N_Com_min", $city);
		$get= $this->db->get();
		$row=$get->result();
		if(!empty($row)) return $row[0]->Cp;
		return false;
	}
	//code departement si on connait codepostal
	//en général on prend 2 chiffres sauf domtom
	public function get_departement_by_cp($codepostal){
		$departement=substr($codepostal, 0, 3);
		$domtom=array(971,972,973,974,975,976,984,986,987,988);
		if(!in_array($departement,$domtom))$departement=substr($codepostal, 0, 2);
		
		return $departement;
	}
	// retrouver l'info de la ville si on connait @ip
	public function getlocationfromDB($ip) {
		//http://dev.maxmind.com/geoip/csv
		/*$o = explode('.',$ip);
			$integer_ip =( 16777216 * $o[0] )+ (65536 * $o[1] )+ (256 * $o[2] );*/
		$sql="SELECT departement_location.city,departement_location.postalCode
				FROM departement_location_ip
				LEFT JOIN departement_location
				ON departement_location.locId=departement_location_ip.locId
				WHERE
				INET_ATON('$ip') BETWEEN startIpNum AND endIpNum
				LIMIT 1";
		$query = $this->db->query($sql);
		if($query->num_rows()){
			$row=$query->result();
			return $row[0];
		}
		return false;
	
	}
}